exports.searchActivityCoupon = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var skip = restfulParams.skip == undefined ? 0 : restfulParams.skip;
    var limit = restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;
    var sort = restfulParams.sort; //1 智能 销量  2 人气
    // if(sort == 1){
    //     var condition = "/sort_order/o_course_sales-desc";
    // }else if(sort == 2){
    //     var condition = "/sort_order/o_browse_times-desc";
    // }
    //任务流创建
    async.waterfall([

            //1.查询附近机构
            function (cb) {
                console.log(12);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchCouponActivity + "/ac_oid/" + restfulParams.oid+"/skip/"+skip+"/limit/"+ limit,
                        method: 'POST',
                        data: {
                            fields: [
                                "ac_oaid",
                                "ac_type",
                                "ac_fid",
                                "ac_oatype",
                                "oa_title",
                                "oa_start_time",
                                "oa_end_time",
                                // "is_cashback"
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.activityCouponList = data.data;

                        cb();
                    }
                );
            },

            //查询优惠券信息
            function (cb) {

                var activityCouponList = resultData.activityCouponList;
                //创建添加任务
                var tasks = [];

                //循环节点列表
                resultData.activityCouponList.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        //代金券
                        if (resultData.activityCouponList[i].ac_type == 0) {

                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_cash_coupon + "/cc_id/ " + item.ac_fid,
                                    method: 'POST',
                                    data: {
                                        fields: ["cc_received_count", "cc_used_count", "cc_start_time", "cc_end_time", "cc_name"].join(",")
                                    }
                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }
                                   // console.log(data.data);
                                    item.couponData = data.data;

                                    cb();
                                }
                            );
                        } else if (resultData.activityCouponList[i].ac_type == 2) {//兑换券
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.searchExchangeCoupon + "/ec_id/ " + item.ac_fid,
                                    method: 'POST',
                                    data: {
                                        fields: ["ec_received_count", "ec_used_count", "ec_start_time", "ec_end_time", "ec_name"].join(",")
                                    }
                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }
                                    // item.coupon = data.data;

                                    item.couponData = data.data;

                                    cb();
                                }
                            );
                        }


                    });

                });

                async.waterfall(tasks, cb);

            },




        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}